<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends AdminBaseController {
    
    public function __construct(){
        parent::__construct();
        $this->new = D("new");
    }
    
    public function index(){
    	$where['title'] = array("like","%".$_GET['title']."%");
        $where['aid'] = array("like","%".$_GET['aid']."%");
        //实例化一个new模型
        $count      = $this->new->where($where)->count();// 查询满足要求的总记录数
        //echo $this->new->getLastSql();
        $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        // $Page -> setConfig('header','共%TOTAL_ROW%条');
        $Page -> setConfig('prev','上一页');
        $Page -> setConfig('next','下一页');
        $show       = $Page->show();// 分页显示输出
        $res = $this->new->where($where)->limit($Page->firstRow.','.$Page->listRows)->getIndex();
    	//$this的使用
        $this->assign("res",$res);
    	$this->assign("page",$show); 
    	// var_dump($res);
    	$this->display();
    }


    //删除数据
    public function del($id){
        if($this->new->del($id)){
            $data['error'] = 1;
        }else{
            $data['error'] = 0; 
        }
        //注意返回的格式
        $this->ajaxReturn($data);
    }



     /**
     * 添加新闻
     */
     public function add(){
        if(IS_GET){
            $this->display();
        }else{
            if($this->new->create()){
                $this->new->created_at= date("Y-m-d H:i:s");
                if($this->new->add()){
                    $this->success("新闻添加成功",U("Index/index"));
                }else{
                    $this->error("新闻添加失败");
                } 
            }else{
                $this->error($this->new->getError());
            }
        }
     }

     public function edit($id){
        if(IS_GET){
            $data = $this->new->edit($id);
            $this->assign("data",$data);
            $this->display();
        }else{
            $this->new->create();
            $data = $this->new->save();
            $data ? $this->success("修改成功",U("Index/index")) : $this->error("修改失败");
        }
    }


}